package cn.knightzz.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author knightzz
 * @date 2021/5/13 16:13
 */
public class ConverterUtils {

    /**
     * 将列表转换为以 符号 分割的字符串
     * [1,2,3,4,5] => "1,2,3,4,5"
     * @param files
     * @param sym
     * @return
     */
    public static String ArrayToStr(List<Long> files, String sym) {
        StringBuffer stringBuffer = new StringBuffer();
        for (Long file : files) {
            stringBuffer.append(file);
            stringBuffer.append(",");
        }
        if (stringBuffer.length() >= 1) {
            // 去除最后一个逗号
            return stringBuffer.substring(0, stringBuffer.length() - 1);
        } else {
            return stringBuffer.toString();
        }
    }

    public static List<Long> StrToList(String files, String sym) {

        List<Long> list = new ArrayList<>();
        String[] split = files.split(",");
        for (String s : split) {
            if(!s.equals("")){
                list.add(Long.parseLong(s));
            }
        }
        return list;
    }
}
